# encoding: utf-8

module Project
  # 跳到项目设置页面
  def show_edit(user, project = $project['project_name'])
    go_project_set(user, project)
  end

  # 定为到公有、私有按钮
  def type_button
    @driver.find_element(:xpath, "//a[@href='#myModa2']")
  end

  # 进入项目成员页面
  def go_member(project_username, project_name)
    show_edit(project_username, project_name)

    @driver.find_element(:link, "项目成员").click
  end

  #项目重命名
  def project_rename(user, project, new_name = false)
    project_name = (project.class == Hash ? project['project_name']: project)
    username = user['username']
    if !new_name then
      new_name = 're_'+project_name
    end

    show_edit(username, project_name)

    @driver.find_element(:id, 'project_name').clear
    @driver.find_element(:id, 'project_name').send_key new_name
    @driver.find_element(:id, 'rename-button').click
  end

  # 修改项目类型（公开、私有）
  def update_type(user, project)
    project_name = (project.class == Hash ? project['project_name']: project)
    username = user['username']

    show_edit(username, project_name)

    type_button.click
    @driver.find_element(:id, "myModa2").find_element(:tag_name, "a").click
  end

=begin

    #项目类型设置
    def type
        show_edit
        @driver.find_element(:xpath, %Q{//a[@data-method='put']}).click
        @driver.switch_to().alert().accept()
    end

    #项目描述设置
    def desc
        show_edit
        @driver.find_element(:name, 'project[description]').send_key(@project['spec'])
        @driver.find_element(:name, 'project[home_page_url]').send_key(@project['home_page'])
        @driver.find_element(:id, 'button2').click
    end

    #删除该用户的所有项目
    def delete_user_all_project
        @driver.get(@base_url)
        all_projects = @driver.find_elements(:css, "#all_projects a strong")
        all_projects.each do |project|
            project_name = project.text
            delete_project(project_name) if project_name
        end
    end
=end

  #删除该用户的所有项目
  def delete_all_project
    projects = Array.new
    go_home
    ll = @driver.find_element(:id, "all_projects").find_elements(:xpath, "//ul/li/a[2]/strong")
    ll.each do |l|
      projects << l.text
    end

    projects.each do |project_name|
      if !project_name.empty? && !$projects['delete_inhibit'].has_value?(project_name) then
        clear_project($login_user, project_name)
      end
    end
  end

  #删除项目
  def delete_project(username, project_name)
      go_info_project_index(username, project_name)

      if @driver.title.include?($config['title_keyword']) then
          show_edit(username, project_name)

          @driver.find_element(:link, "删 除").click
          @driver.find_element(:xpath, %Q{//a[@data-method='delete']}).click
      end
  end

  # 删除项目
  def clear_project(user, project)
    username = user['username']
    project_name = (project.class == Hash ? project['project_name']: project)

    delete_project(username, project_name)
  end

  # 创建项目
  # project["project_name"] => 项目名称
  # project["type"] => 项目类型{"private" => "私有项目"}
  # project["is_readme"] => 是否使用readme生成项目,false不选择readme

  def create_project(project, is_delete = true, is_while = true)
    # 是否删除该项目
    if is_delete then
      go_project_index($login_user, project)
      if element_present?(:css, ".logo.pull-left") then
        clear_project($login_user, project)
      end
    end
    @driver.get(@base_url + "/projects/new")
    @driver.find_element(:id, "project_name").clear
    @driver.find_element(:id, "project_name").send_keys project['project_name']
    # 判断项目类型
    if project.has_key?("type") && project["type"] == 'private' then
        @driver.find_element(:id, "project_public_false").click
    end

    # 判断项目类型
    if project.has_key?("is_readme") && !project["is_readme"] then
      @driver.find_element(:id, "project_is_generate_readme").click
    end

    @driver.find_element(:name, "commit").click

    # 是否验证项目创建成功
    while is_while
      if @driver.title.include?("/projects/new") then
        sleep 1
      else
        is_while = false
      end
    end

  end

  #导入项目
  def import(project, is_delete = true)
    # 是否删除该项目
    if is_delete then
      go_project_index($login_user, project)
      if element_present?(:css, ".logo.pull-left") then
        clear_project($login_user, project)
      end
    end

    @driver.get(@base_url + "/projects/import")
    @driver.find_element(:id, "project_import_url").clear
    @driver.find_element(:id, "project_import_url").send_keys project['git_url']
    @driver.find_element(:name, "commit").click

    @dri
  end




end
